import collections


class Solution(object):
    def evaluate(self, expression):
        state = 0
        p = []
        st = []
        left = 0
        for index, c in enumerate(expression):
            if state == 0:
                if c == '(':
                    state = 1
                    left = index + 1
            else:
                if state == 1:
                    if c == ' ':
                        key = expression[left: index]
                        if key == 'let':
                            p.append({})
                        st.append([key])
                        state = 2
                        left = index + 1
                else:
                    if state == 2:
                        if c == ' ':
                            key = expression[left: index]
                            left = index + 1
                            if key != '':
                                if st[-1][0] == 'let':
                                    if len(st[-1]) == 2:
                                        node = st[-1].pop()
                                        try:
                                            p[-1][node] = int(key)
                                        except:
                                            for i in range(len(p) - 1, -1, -1):
                                                if key in p[i]:
                                                    p[-1][node] = p[i][key]
                                    else:
                                        st[-1].append(key)
                                else:
                                    st[-1].append(key)
                        if c == ')':
                            key = expression[left: index]
                            left = index + 1
                            st[-1].append(key)
                            last_st = st.pop()
                            if last_st[0] == 'let':
                                val = last_st[1]
                                try:
                                    val = int(val)
                                except:
                                    for i in range(len(p) - 1, -1, -1):
                                        if val in p[i]:
                                            val = p[i][val]
                                p.pop()
                            else:
                                val1 = last_st[1]
                                try:
                                    val1 = int(val1)
                                except:
                                    for i in range(len(p) - 1, -1, -1):
                                        if val1 in p[i]:
                                            val1 = p[i][val1]
                                val2 = last_st[2]
                                try:
                                    val2 = int(val2)
                                except:
                                    for i in range(len(p) - 1, -1, -1):
                                        if val2 in p[i]:
                                            val2 = p[i][val2]
                                if last_st[0] == 'add':
                                    val = val1 + val2
                                else:
                                    val = val1 * val2
                            if not st:
                                return val
                            else:
                                if st[-1][0] == 'let':
                                    if len(st[-1]) == 2:
                                        node = st[-1].pop()
                                        p[-1][node] = val
                                    else:
                                        st[-1].append(val)
                                else:
                                    st[-1].append(val)
                    if c == '(':
                        state = 1
                        left = index + 1


data = Solution()
# expression = "(let x 3 y 2 (add x y))"
# print(data.evaluate(expression))
# expression = "(let x 2 (mult x (let x 3 y 4 (add x y))))"
# print(data.evaluate(expression))
# expression = "(let x 2 (add (let x 3 (let x 4 x)) x))"
# print(data.evaluate(expression))
# expression = "(add 1 2)"
# print(data.evaluate(expression))
# expression = "(let x -2 y x y)"
# print(data.evaluate(expression))
expression = "(let var 78 b 77 (let c 33 (add c (mult var 66))))"
print(data.evaluate(expression))
